import { service } from './config'
import util from '@/assets/js/util.js'
const baseUrl = __DEV__ ? `${import.meta.env.VITE_DOMAIN_PATH}` : `${window.location.origin}`
export default {
  postAction(url: string, params = {}, headers = {}, isJson = true) { // 默认不传头，默认为json
    // url = baseUrl + url
    const config = {
      url,
      method: 'POST',
      headers: { 'Content-Type': isJson ? 'application/json' : 'application/x-www-form-urlencoded;charset=UTF-8' },
      data: params
    }
    headers && Object.assign(config.headers, headers)
    return service.request(config).then((response: any) => {
      if (response.status===200 || response.status===302) {
        return Promise.resolve(response.data)
      } else {
        return Promise.reject(
          util.isEmpty(response.data) || util.isEmpty(response.data.errorMessage) ? {
            errorMessage: '操作失败！'
          } : response.data
        )
      }
    }).catch(error => {
      return Promise.reject(error)
    })
  },
  getAction (url: string) {
    // url = baseUrl + url
    return service.get(url,{
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((response: any) => {
      if (response.status===200 || response.status===302) {
        return Promise.resolve(response.data)
      } else {
        return Promise.reject(
          util.isEmpty(response.data) || util.isEmpty(response.data.errorMessage) ? {
            errorMessage: '操作失败！'
          } : response.data
        )
      }
    }).catch(error => {
      return Promise.reject(error)
    })
  },
  putAction (url: string, params = {}) {
    // url = baseUrl + url
    return service.request({
      url,
      data: params,
      method: 'put',
      headers: { 'Content-Type': 'application/json' }
    }).then((response: any) => {
      if (response.status===200 || response.status===302) {
        return Promise.resolve(response.data)
      } else {
        return Promise.reject(
          util.isEmpty(response.data) || util.isEmpty(response.data.errorMessage) ? {
            errorMessage: '操作失败！'
          } : response.data
        )
      }
    })
    .catch(error => {
      return Promise.reject(error)
    })
  },
  postBlobAction (url: string, params = {}) {
    // responseType: 'blob'，不加返回的是乱码
    // url = baseUrl + url
    return service.request({
      url,
      data: params,
      responseType: 'blob',
      method: 'post',
      headers: { 
        'Content-Type': 'application/json'
      },
    }).then((response: any) => {
      if (response.status===200 || response.status===302) {
        return Promise.resolve(response)
      } else {
        return Promise.reject(
          util.isEmpty(response.data) || util.isEmpty(response.data.errorMessage) ? {
            errorMessage: '操作失败！'
          } : response.data
        )
      }
    }).catch(error => {
      return Promise.reject(error)
    })
  },
  getDownloadAction (url) {
    // url = baseUrl + url
    return service.get(url, {
        responseType: 'blob'
      })
      .then(response => {
        if (response.status===200 || response.status===302) {
          return Promise.resolve(response)
        } else {
          return Promise.reject(
            util.isEmpty(response.data) || util.isEmpty(response.data.errorMessage) ? {
              errorMessage: '操作失败！'
            } : response.data
          )
        }
      }).catch(error => {
        return Promise.reject(error)
      })
  }
}

